﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
using System.Data;

namespace CRM
{
    public partial class Data_Together : System.Web.UI.Page
    {
        protected override void OnPreLoad(EventArgs e)
        {
            Common.CheckMes.CheckState("User_ID", "Index.aspx", "用户登录信息丢失，请重新登录...");
            string PowerMes = Common.EncryptString.DeMyEnncrypt(Common.GetMes.GetCookies("Power_Mes"), 6);

            if (PowerMes.IndexOf("3_3") < 0)
            {
                Common.MessageBox.Show(Page, "你没有该功能的权限...", "Body.aspx", "BACK");
            }
            base.OnPreLoad(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void BtnTogether_Click(object sender, EventArgs e)
        {
            //总记录数量
            int Totail = BLL.SysBase.GetUserCount();
            int PageIndexCount = (string.IsNullOrEmpty(Common.FileComm.ReadFiles(Server.MapPath("~/UpLoad/PageIndex.txt")))) ? 0 : Convert.ToInt32(Common.FileComm.ReadFiles(Server.MapPath("~/UpLoad/PageIndex.txt")));
            Model.DataTogther DataModel = new CRM.Model.DataTogther();
            DataModel.TableName = " [user] ";
            DataModel.FieldList = " [id],[person],[sex],[name],[tel],[fax],[shouji] ,[email],[address],[yiubian],[qq],[qyurl],[ntime],[shengfen],[gjhy],[kefu] ";
            DataModel.Where = "";
            DataModel.Order = "[id] ASC ";
            DataModel.PageSize = 20;
            DataModel.PageIndex = PageIndexCount;
            DataTable DTData = null;
            string NewUserID = string.Empty;
            int AllCount = ((Totail % 20) > 0) ? (Totail / 20) + 1 : Totail / 20;
            AllCount = AllCount - PageIndexCount;
            Model.DataUserInfo UserModel = new CRM.Model.DataUserInfo();
            for (int j = 0; j < AllCount; j++)
            {
                DataModel.PageSize = 20;
                DataModel.PageIndex = j + 1;
                DTData = BLL.SysBase.DownData(DataModel, DBUtility.SqlHelper.sqlConnectionStringService);
                if (DTData.Rows.Count > 0)
                {
                    UserModel.T_UserType = 1;
                    UserModel.T_UserFrom = 0;
                    UserModel.T_Remark = "";
                    UserModel.T_Province = "";
                    UserModel.T_City = "";
                    UserModel.T_UserID = 0;
                    for (int i = 0; i < DTData.Rows.Count; i++)
                    {
                        UserModel.T_Person = DTData.Rows[i]["person"].ToString();
                        UserModel.T_Sex = DTData.Rows[i]["sex"].ToString();
                        UserModel.T_CompanyName = DTData.Rows[i]["name"].ToString();
                        UserModel.T_Tel = DTData.Rows[i]["tel"].ToString();
                        UserModel.T_Fax = DTData.Rows[i]["fax"].ToString();
                        UserModel.T_Mobile = DTData.Rows[i]["shouji"].ToString();
                        UserModel.T_Email = DTData.Rows[i]["email"].ToString();
                        UserModel.T_Address = DTData.Rows[i]["address"].ToString();
                        UserModel.T_PostCode = DTData.Rows[i]["yiubian"].ToString();
                        UserModel.T_QQ = DTData.Rows[i]["qq"].ToString();
                        UserModel.T_WebUrl = DTData.Rows[i]["qyurl"].ToString();
                        UserModel.T_Province = DTData.Rows[i]["shengfen"].ToString();
                        UserModel.T_NowService = DTData.Rows[i]["kefu"].ToString();
                        UserModel.T_UserLeave = (string.IsNullOrEmpty(DTData.Rows[i]["gjhy"].ToString())) ? 0 : Convert.ToInt32(DTData.Rows[i]["gjhy"].ToString());
                        UserModel.T_DateTime = (string.IsNullOrEmpty(DTData.Rows[i]["ntime"].ToString())) ? DateTime.Now : Convert.ToDateTime(DTData.Rows[i]["ntime"].ToString());
                        UserModel.T_UserState = GetUserState(DTData.Rows[i]["gjhy"].ToString(), DTData.Rows[i]["kefu"].ToString());
                        BLL.SysBase.AddNewsData(UserModel, out NewUserID);
                    }
                    BLL.SysBase.WriteFile(Server.MapPath("~/UpLoad/PageIndex.txt"), Convert.ToString(++PageIndexCount));
                }
                DTData.Clear();
                DTData.Dispose();
            }
            Common.MessageBox.myScriptMes(Page, "CloaseOver()", "Suess");
        }


        protected void BtnTogetherCom_Click(object sender, EventArgs e)
        {
            //总记录数量
            int Totail = BLL.SysBase.GetComCount();
            int PageIndexCount = (string.IsNullOrEmpty(Common.FileComm.ReadFiles(Server.MapPath("~/UpLoad/PageIndexCom.txt")))) ? 1 : Convert.ToInt32(Common.FileComm.ReadFiles(Server.MapPath("~/UpLoad/PageIndexCom.txt")));
            Model.DataTogther DataModel = new CRM.Model.DataTogther();
            DataModel.TableName = " [scqy] ";
            DataModel.FieldList = " [id],[fddbr],[qyfzr],[zcdz],[qymc],[tel],[fax],[email],[scdz],[yiubian],[url],[fzrq],[sf] ";
            DataModel.Where = "";
            DataModel.Order = "[id] ASC ";
            DataModel.PageSize = 20;
            DataModel.PageIndex = PageIndexCount;
            DataTable DTData = null;
            string NewUserID = string.Empty;
            int AllCount = ((Totail % 20) > 0) ? (Totail / 20) + 1 : Totail / 20;
            AllCount = AllCount - PageIndexCount;
            Model.DataUserInfo UserModel = new CRM.Model.DataUserInfo();
            for (int j = 0; j < AllCount; j++)
            {
                DataModel.PageSize = 20;
                DataModel.PageIndex = j + 1;
                DTData = BLL.SysBase.DownData(DataModel,DBUtility.SqlHelper.sqlConnectionStringZYQY);
                if (DTData.Rows.Count > 0)
                {
                    UserModel.T_UserLeave = 0;
                    UserModel.T_UserState = 0;
                    UserModel.T_UserType = 1;
                    UserModel.T_UserFrom = 0;
                    UserModel.T_Remark = "";
                    UserModel.T_Province = "";
                    UserModel.T_City = "";
                    UserModel.T_UserID = 0;
                    UserModel.T_NowService = "";
                    for (int i = 0; i < DTData.Rows.Count; i++)
                    {
                        UserModel.T_Person = DTData.Rows[i]["qyfzr"].ToString() + " [法人] " + DTData.Rows[i]["fddbr"].ToString();
                        UserModel.T_CompanyName = DTData.Rows[i]["qymc"].ToString();
                        UserModel.T_Tel = DTData.Rows[i]["tel"].ToString();
                        UserModel.T_Fax = DTData.Rows[i]["fax"].ToString();
                        UserModel.T_Email = DTData.Rows[i]["email"].ToString();
                        UserModel.T_Address = DTData.Rows[i]["zcdz"].ToString() + " [生产] " + DTData.Rows[i]["scdz"].ToString();
                        UserModel.T_PostCode = DTData.Rows[i]["yiubian"].ToString();
                        UserModel.T_WebUrl = DTData.Rows[i]["url"].ToString();
                        UserModel.T_Province = DTData.Rows[i]["sf"].ToString();
                        UserModel.T_DateTime = (string.IsNullOrEmpty(DTData.Rows[i]["fzrq"].ToString())) ? DateTime.Now : Convert.ToDateTime(DTData.Rows[i]["fzrq"].ToString());
                        BLL.SysBase.AddNewsData(UserModel, out NewUserID);
                    }
                    BLL.SysBase.WriteFile(Server.MapPath("~/UpLoad/PageIndexCom.txt"), Convert.ToString(++PageIndexCount));
                }
                DTData.Clear();
                DTData.Dispose();
            }
            Common.MessageBox.myScriptMes(Page, "CloaseOver()", "Suess");
        }

        /// <summary>
        /// 返回状态
        /// </summary>
        /// <param name="strGJHY"></param>
        /// <returns></returns>
        private int GetUserState(string strGJHY,string strService)
        {
            int State = 0;
            if (!string.IsNullOrEmpty(strGJHY))
            {
                if (strGJHY == "1")
                {
                    State = 2;
                }
                else if (strGJHY == "0")
                {
                    State = 0;
                }
                else
                {
                    State = 0;
                }
            }

            if (!string.IsNullOrEmpty(strService))
            {
                State = 2;
            }
            return State;
        }
    }
}
